﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogicBase;
using CL3_IF_DllSample;

namespace CL_Native
{
 public   class CL_Intial:Logic
    {
        [TextBox, GroupBox("Input")]
        public lgcExpString IP { get; set; } = new lgcExpString();
        [TextBox, GroupBox("Input")]
        public lgcExpString Port { get; set; } = new lgcExpString();
             
        public CL_Intial()
        {

        }
        public override Result ToolDo(RunNode runNode, RunControl runControl)
        {
            CL3IF_ETHERNET_SETTING ethernetSetting = new CL3IF_ETHERNET_SETTING();
            ethernetSetting.ipAddress = new byte[4];
            string[] IPs = IP.getString().Split('.');
            if (!byte.TryParse(IPs[0], out ethernetSetting.ipAddress[0]))
            {
                ShowLog.Error("IP error");
                return Result.ERR;
            }
            if (!byte.TryParse(IPs[1], out ethernetSetting.ipAddress[1]))
            {
                ShowLog.Error("IP error");
                return Result.ERR;
            }
            if (!byte.TryParse(IPs[2], out ethernetSetting.ipAddress[2]))
            {
                ShowLog.Error("IP error");
                return Result.ERR;
            }
            if (!byte.TryParse(IPs[3], out ethernetSetting.ipAddress[3]))
            {
                ShowLog.Error("IP error");
                return Result.ERR;
            }
            if (!ushort.TryParse(Port.getString(), out ethernetSetting.portNo))
            {
                ShowLog.Error("portNo error");
                return Result.ERR;
            }

            ethernetSetting.reserved = new byte[2];
            ethernetSetting.reserved[0] = 0x00;
            ethernetSetting.reserved[1] = 0x00;
            int returnCode = NativeMethods.CL3IF_OpenEthernetCommunication(0, ref ethernetSetting, 10000);

         //   OutputLogMessage("OpenEthernetCommunication", returnCode);

           // SetDeviceStatement(returnCode, DeviceStatus.Ethernet);
          //  _deviceData[CurrentDeviceId].EthernetSetting = ethernetSetting;

            return Result.OK;
        }
    }
}
